package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.config.exceptionhandler.GuliException;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-02
 */

@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;


    //1、查询讲师全部数据
    //地址：http://localhost:8001/eduservice/teacher/findAll
    @ApiOperation("获取所有列表")
    @GetMapping("findAll")
    public R findAllTeacher() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }


    //2、讲师逻辑删除功能
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除讲师")
    //路径传递参数id
    public R removeById(@ApiParam(name="id",value = "讲师ID",required = true) @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    //3、分页查询讲师
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page, limit);
        //调用方法实现分页
        //调用方法时。底层封装，把分页所有数据封装到pageteacher
        teacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    //4、条件查询讲师带分页
    @ApiOperation(value = "条件分页讲师列表")
    @PostMapping("{current}/{limit}")
    public R pageTeacherCondition (
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断条件值是否为空,如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified",end);
        }

        //调用page方法实现分页查询
        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    //5、添加讲师信息
    @ApiOperation(value = "新增讲师")
    @PostMapping()
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //6、根据讲师id进行查询
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);

//        try {
//            int i= 1/0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"执行力GuliException");
//        }

        return R.ok().data("item", eduTeacher);
    }

    //7、修改讲师信息
    @ApiOperation(value = "根据id修改讲师")
    @PutMapping("{id}")
    public R updateTeacher(
            @ApiParam(name="id",value = "讲师ID",required = true)
            @PathVariable String id,
            @ApiParam(name="teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {

        eduTeacher.setId(id);
        boolean flag = teacherService.updateById(eduTeacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }






}

